Skip to content

Adding dukglue_register_function_property#21

Open
CallumCarmicheal wants to merge 1 commit intoAloshi:masterfrom
CallumCarmicheal:patch-1
Open

Adding dukglue_register_function_property#21
CallumCarmicheal wants to merge 1 commit intoAloshi:masterfrom
CallumCarmicheal:patch-1

Conversation

@CallumCarmicheal
Copy link
Copy Markdown

@CallumCarmicheal CallumCarmicheal commented Aug 17, 2019

A helper function to allow the use of dukglue on Objects instead of the Global namespace.

Example code usage:

int math_add(int x, int y) {
    printf("Example function called\n");
    return x + y;
}

void CreateTheObject(duk_context *ctx) {
    // 1. Create a new object
    // 2. Register the function as a property on the object
    // 3. Store the object as global
    duk_push_object(ctx);
    dukglue_register_function_property(ctx, &math_add, "add");
    duk_put_global_string(ctx, "Math");
}

void CreateTheObjectWithIndex(duk_context *ctx) {
    // 1. Create a new object
    // 2. Register the function as a property on the object
    // 3. Store the object as global
    duk_idx_t mathObjectIdx = duk_push_object(ctx);

    // lets say for what ever reason there are more values on the stack
    for(int x = 0; x < 3; x++)
        duk_push_null(ctx);

    // We will now register the function to the original stack object's index (mathObjectIdx)
    dukglue_register_function_property(ctx, &math_add, mathObjectIdx, "add");

    // Clean up the nulls (only in the example)
    for(int x = 0; x < 3; x++)
        duk_pop(ctx);

    duk_put_global_string(ctx, "Math");
}

int PlayerOnHit(int currentHealth) {
    return currentHealth--;
}

void AppendToObject(duk_context *ctx) {
    // 1. Find the global object "Player"
    // 2. Register the function as a property
    // 3. Remove "Game" from the stack 
    duk_get_global_string(ctx, "Game");
    dukglue_register_function_property(ctx, &PlayerOnHit, "OnHit");
    duk_pop(ctx);
}

A helper function to allow the use of dukglue on Objects instead of the Global namespace.

Example code usage:

```cpp
int math_add(int x, int y) {
    printf("Example function called\n");
    return x + y;
}

void CreateTheObject(duk_context *ctx) {
    // 1. Create a new object
    // 2. Register the function as a property on the object
    // 3. Store the object as global
    duk_push_object(ctx);
    dukglue_register_function_property(ctx, &math_add, "add");
    duk_put_global_string(ctx, "Math");
}

void CreateTheObjectWithIndex(duk_context *ctx) {
    // 1. Create a new object
    // 2. Register the function as a property on the object
    // 3. Store the object as global
    duk_idx_t mathObjectIdx = duk_push_object(ctx);

    // lets say for what ever reason there are more values on the stack
    for(int x = 0; x < 3; x++)
        duk_push_null(ctx);

    // We will now register the function to the original stack object's index (mathObjectIdx)
    dukglue_register_function_property(ctx, &math_add, mathObjectIdx, "add");
    duk_put_global_string(ctx, "Math");

    // Clean up the nulls (only in the example)
    for(int x = 0; x < 3; x++)
        duk_pop(ctx);
}

int PlayerOnHit(int currentHealth) {
    return currentHealth--;
}

void AppendToObject(duk_context *ctx) {
    // 1. Find the global object "Player"
    // 2. Register the function as a property
    // 3. Remove "Game" from the stack 
    duk_get_global_string(ctx, "Game");
    dukglue_register_function_property(ctx, &PlayerOnHit, "OnHit");
    duk_pop(ctx);
}
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant